﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.DomainServices.Hosting;
using RIAModels;
using RiaToDalLinq;
using System.ComponentModel.Composition.Hosting;
using RiaToDalLinq.Attributes;
using RiaToDalLinq.DomainServices;
using RiaToDalLinq.Enumeration;

namespace RIAServicesLegacyDalIntegration.Web.Services
{
    [EnableClientAccess]
    public class GetDataService : LegacyDalDomainServiceBase
    {
        // Create the query context
        RiaToDalQueryContext<RIAUser> RIAUserQueryContext;

        public GetDataService()
        {
            // Instantiate the context with this domain service
            RIAUserQueryContext = new RiaToDalQueryContext<RIAUser>(this);
        }

        /// <summary>
        /// MethodMap will map our queryable GetUser() ria method to the GetUsersByNameAndSearch in our legacy dal.
        /// It will look for an overload which takes a name parameter.
        /// The full method signature in the legacy DAL looks like:
        ///     GetUsersByNameAndSearch(string name, string whereClause, string orderBy, int pageIndex, int pageSize)
        /// but by convention any parameter name of whereClause, orderBy, pageIndex, pageSize will be excluded from the match and
        /// will be passed data based on the LINQ query that is composed.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [WhereClauseParameter(Name="whereClause", DefaultValue = "1=1")]
        [PageStartParameter(Name="pageIndex", ActAs = PageStartActAs.PageIndex)]    // When ActAs = PageIndex the page index will be calculated and sent to the DAL
                                                                                    // When ActAs = Skip the skip value will be sent to the DAL
        [StaticParameter(Name="someStaticParameter", Value=7)]
        [MethodMap("GetUsersByNameAndSearch")]
        public IQueryable<RIAUser> GetUser(
            string name,
            [CodeToIdMap(typeof(object), "UserStatusCode", "UserStatusId")]         // Converts a UserStatusCode to a UserStatusID before the GetUsersByNameAndSearch() is called
                                                                                    // This allows us to easily take a code value from the client, do a lookup, and pass the ID value into the legacy DAL method
            string randomUserStatusCode
            )
        {
            // Use some LINQ against our RIA entity. The Entities property is already filtered by the "name" parameter of the current query method,
            // but we can do some additional filtering, and it will be propagated through to the whereClause in the legacy DAL method.
            var query = from u in RIAUserQueryContext.Entities
                        where (u.RiaFirstName != "Jim" && u.RiaUserStatus == "Active") || u.RiaFirstName == "Ronnie"
                        orderby u.RiaFirstName descending
                        select u;

            // Return the query, let RIA services compose any additional LINQ sent from the client
            return query;
        }
    }
}